<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="">
                    <el-input class="w-[280px]" v-model="queryParams.keyword" placeholder="昵称/手机号码" clearable @keyup.enter="resetPage"/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-table size="small" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="编号" prop="id" min-width="70" />
                <el-table-column label="昵称" prop="realName" min-width="100" />
                <el-table-column label="归属地" prop="areaname" min-width="100" />
                <el-table-column label="管理公司" prop="company" min-width="100" />
                <el-table-column label="账号" prop="username" min-width="120" />
                <el-table-column label="账号状态" prop="isDisable" min-width="80" align="center">
                    <template #default="{ row }">
                        <dict-value :options="dictData.is_disable" :value="row.isDisable" />
                    </template>
                </el-table-column>
                <el-table-column label="头像" min-width="100" align="center">
                    <template #default="{ row }">
                        <el-avatar :src="row.avatar" :size="50" />
                    </template>
                </el-table-column>
                <el-table-column label="性别" prop="sex" min-width="50" align="center">
                    <template #default="{ row }">
                        <dict-value :options="dictData.sex" :value="row.sex" />
                    </template>
                </el-table-column>
                <el-table-column label="角色" prop="isLawyer" min-width="120" align="center">
                    <template #default="{ row }">
                        <dict-value :options="dictData.user_role" :value="row.isLawyer" />
                    </template>
                </el-table-column>
                <el-table-column label="AI用户" prop="aiTokenband" min-width="120" />
                <el-table-column label="对话余额" prop="" min-width="120" />
                <el-table-column label="推荐人" prop="cooperatorUserName" min-width="120" />
                <el-table-column label="操作" width="200" fixed="right">
                    <template #default="{ row }">
                        <el-button v-if="row.aiTokenband != '已绑定'" type="primary" link @click="handleBalance(row)">
                            编辑对话余额
                        </el-button>
                        <el-button type="primary" link @click="handleRole(row)">
                            修改角色
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="consumerLists">
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'
import { getUserList } from '@/api/consumer'
import { ClientMap } from '@/enums/appEnums'
import { useDictData } from "@/hooks/useDictOptions";
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()

const queryParams = reactive({
    keyword: '',
    channel: '',
    startTime: '',
    endTime: ''
})
const showEdit = ref(false)
const { dictData } = useDictData<{
    is_disable: any[],
    sex: any[],
    user_role: any[]
}>(['is_disable', 'sex', 'user_role'])

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getUserList,
    params: queryParams
})
onActivated(() => {
    getLists()
})

const handleBalance = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('handleBalance')
    editRef.value?.setFormData(data)
}

const handleRole = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('handleRole')
    editRef.value?.setFormData(data)
}

getLists()
</script>
